package demo5;

import java.util.Arrays;

public class MyStack {
    public int[] arr;
    public int usedSize;
    public int MAX_SIZE = 10;
    public MyStack() {
        arr = new int[MAX_SIZE];
    }
    public void push(int data) {
        if(isFull()) {
            //扩容
            arr = Arrays.copyOf(arr,arr.length * 2);
        }
        arr[usedSize++] = data;
    }
    public boolean isFull() {
        return usedSize == arr.length;
    }
    public boolean isEmpty() {
        return usedSize == 0;
    }
    public int pop() {
        if(isEmpty()) {
            throw new NullException("栈为空!");
        }
        usedSize--;
        return arr[usedSize];
    }
    public int peek() {
        if(isEmpty()) {
            throw new NullException("栈为空!");
        }
        return arr[usedSize - 1];
    }
    public int size() {
        return usedSize;
    }

    public static void main(String[] args) {
        MyStack stack = new MyStack();
        stack.push(2);
        stack.push(1);
        stack.push(3);
        stack.push(4);
        System.out.println(stack.size());
        System.out.println(stack.pop());
        System.out.println(stack.size());
    }
}
